__author__ = 'Kerr Wang'

import struct
import time
import logging

def hex_to_string(hexNum):
	if not isinstance(hexNum, (int)):
		raise TypeError('bad operand type, parameter must be a int')
	char_tuple = ('0','1','2','3','4','5','6','7','8','9',' ',' ',' ',' ',' ',' ')
	first_char = int(hexNum / 16)
	second_char = int(hexNum % 16)
	return char_tuple[first_char]+char_tuple[second_char]

def id_to_string(idData, replace=False):
	id_str = ''
	idData_list = []
	if not isinstance(idData, (list, tuple)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	for i in range(0, len(idData)):
		idData_list.append(idData[i])
	if replace == True:
		idData_list[0] = idData_list[0]&0x7F
	for data in idData_list:
		id_str = id_str + hex_to_string(data)
	return id_str.rstrip()

def is_replace(idData):
	if not isinstance(idData, (list, tuple)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	if idData[0]&0x80 == 0:
		return False
	else:
		return True

def is_beat(idData):
	if not isinstance(idData, (list, tuple)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	if idData[-3] == 0xFD:
		return True
	else:
		return False

def data_correct(data, recordLenth):
	if not isinstance (data, (tuple, list)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	if len(data) < recordLenth:
		return False
	if  data[-1]!=0xFE:
		return False
	if data[-2]!=0:
		if (len(data)-3)/recordLenth != data[-2]:
			return False
	return True

def record_ana (dataRecord):
	if dataRecord[0]!=0x0A and dataRecord[-1]!=0x0B:
		return False
	else:
		id_len = dataRecord[1]-2
		id_str = id_to_string(dataRecord[2:id_len+2], is_replace(dataRecord[2:id_len+2]))
		machine_num = dataRecord[-3]
		count = dataRecord[-2]
		stu_per = (id_str, machine_num, is_replace(dataRecord[2:id_len+2]),count)
		return stu_per

def data_analyze(data, recordLenth):
	stu_list = []
	list_len = len(data)

	index = 0
	data_length = 0
	if not isinstance (data, (tuple, list)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	while index < list_len-3:
		if data[index]!=0x0A:
			index += 1
		else:
			data_length = data[index+1]
			if (index+data_length+3 < list_len):
				if data[index+data_length+2]!=0x0B:
					index += 1
				else:
					L = data[index:index+data_length+3]
					id_len = L[1]-2
					id_str = id_to_string(L[2:id_len+2], is_replace(L[2:id_len+2]))
					machine_num = L[-3]
					count = L[-2]
					stu_per = (id_str, machine_num, is_replace(L[2:id_len+2]),count)
					stu_list.append(stu_per)
					index += 1
			else:
				index += 1
	return stu_list, data[-3]

def data_analyze2(data, recordLenth):
#	machine_num_tuple = ('A','B','C','D','E','F','F','G','H','I','J','K','L','M','N')
	stu_list = []
	if not isinstance (data, (tuple, list)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	for i in range(0, len(data)-recordLenth, recordLenth):
		L = data[i:i+recordLenth]
		if L[0]!=0x0A and L[-1]!=0x0B:
			continue
		else:
			id_len = L[1]-2
			id_str = id_to_string(L[2:id_len+2], is_replace(L[2:id_len+2]))
			machine_num = L[-3]
			count = L[-2]
			stu_per = (id_str, machine_num, is_replace(L[2:id_len+2]),count)
			stu_list.append(stu_per)
	return stu_list, data[-3]
	
def main():	
	t = (0x0A, 0x0A, 0x20, 0x16, 0x12, 0x20, 0x09, 0x03, 0x01, 0x3F, 0x44, 0xE4, 0x0B, 
		 0x0A, 0x0A, 0x20, 0x16, 0x23, 0x20, 0x09, 0x02, 0x00, 0x2F, 0x44, 0xE5, 0x0B, 
		 0x0A, 0x0A, 0x20, 0x16, 0x12, 0x20, 0x24, 0x01, 0x00, 0x3F, 0x44, 0xE6, 0x0B, 
		 0xB1, 0x03, 0xFE)
	print(data_analyze(t, 13))

if __name__ == '__main__':
	main()
